package approve.action;

import java.util.Timer;

import project.active.LogoutActivity;

import resource.pojo.Approvedoc;
import model.pojo.Document;
import model.pojo.Employee;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import approval.dao.ApproveDocumentDAO;

public class My_ViewForm14 extends Activity {
	TextView dailyreport_title,la_description,la_content,la_title,valTitle,valDescr,valContent;
//	EditText title,description;
//	WebView content;
	
	Button bttCancel,bttDelete;
	
	//bundle
		Approvedoc AppDoc=null;	
		String database = "admin";	
		String lang = "EN";
		Bundle language=null;
		Employee emp=new Employee();	
	
		Timer timer=new Timer();
		NotificationManager mNotificationManager=null;
		private int SIMPLE_NOTFICATION_ID;
		TextView logout,username;
		
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_viewform14);
		
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		loadBundle();
		if (getIntent().getExtras().getString("delete") != null) {
			LinearLayout linearDelete = (LinearLayout) findViewById(R.id.linearDelete);
			Button btnDelete = new Button(this);
			btnDelete.setText(language.getString("Delete"));
			btnDelete.setOnClickListener(selectDelete);
			linearDelete.addView(btnDelete);
			linearDelete.setVisibility(View.VISIBLE);
		}
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		getComponent();
		loadLanguage();
		
		
	}
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
//			Intent i=new Intent(MainFrame.this, LoginActivity.class);			
			timer.cancel();
			mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
			
			Intent i=new Intent(My_ViewForm14.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	
	private void loadBundle(){
		Bundle extras= getIntent().getExtras();
		if(extras!=null){
			database=extras.getString("database");
			lang=extras.getString("lang");
			emp=(Employee) extras.get("em");
			AppDoc=(Approvedoc) extras.get("AppDoc");	
			language=extras.getBundle(lang);
		}
	}
	private void getComponent(){
		la_title=(TextView) findViewById(R.id.la_title);
		dailyreport_title=(TextView) findViewById(R.id.dailyreport_title);
		la_description=(TextView) findViewById(R.id.la_description);
		la_content=(TextView) findViewById(R.id.la_content);
		
		
		
		valTitle = (TextView)findViewById(R.id.valueTitleMydocument);
		valDescr = (TextView)findViewById(R.id.valueDecriptionMydocument);
		valContent = (TextView)findViewById(R.id.valueContentMydocument);
		
		
		valTitle.setText(AppDoc.getTitle());
		valDescr.setText(AppDoc.getDescription());
		valContent.setText(Html.fromHtml(AppDoc.getContent()));
		
//		title=(EditText) findViewById(R.id.dailyreport_title);
//		description=(EditText) findViewById(R.id.dailyreport_description);
//		content=(WebView) findViewById(R.id.dailyreport_content);
	
//		title.setText(AppDoc.getTitle());
//		description.setText(AppDoc.getDescription());
//		final String mimeType = "text/html";
//		String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
//		content.loadData(header+AppDoc.getContent(), mimeType, null);
		
		
		
	}
	private void loadLanguage(){
		if(language!=null){
			la_title.setText(language.getString("title") + ":");
			la_description.setText(language.getString("Description") + ":");
			la_content.setText(language.getString("content") + ":");
		}
	}
	OnClickListener selectDelete = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			dialogDelete();
		}
	};

	private void dialogDelete() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(language.getString("Delete"));
		alert.setMessage(language.getString("are-you-sure"));
		alert.setPositiveButton(language.getString("ok"),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						ApproveDocumentDAO.moveApproveToTrash(AppDoc.getiDApproveDoc(),
								database);
						goToDeclineDocument();
					}
				});
		alert.setNegativeButton(language.getString("cancel"),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
					}
				});

		AlertDialog dialog = alert.create();
		dialog.show();
	}

	private void goToDeclineDocument() {
		finish();
		Intent intent = new Intent(My_ViewForm14.this,
				My_App_DeclineDocument.class);
		Bundle extras = getIntent().getExtras();
		try{
			extras.remove("delete");
			extras.remove("AppDocID");
		}catch (Exception e) {
		}
		intent.putExtras(extras);
		startActivity(intent);
	}
	
	
	
}
